package com.hbnu.ysc;


import java.util.Arrays;
import java.util.OptionalInt;
import java.util.Scanner;

/**
 * 实现一个简易银行账户管理系统，要求：
 * 1. 使用数组存储多个账户的余额
 * 2. 实现存款、取款和转账功能
 * 3. 取款时检查余额是否充足
 * 4. 统计所有账户的总余额
 * 5. 找出余额最多和最少的账户
 */
public class BankAccountManager {
    public static void main(String[] args) {

        System.out.println();
        System.out.println("+++++++++++++++++++++++++++欢迎进入银行管理系统+++++++++++++++++++++++++++");
        System.out.println();
        int[] arr = {100,150,200,300,100};

        for (int i = 0; i < 5; i++) {
            System.out.println("账户："+(i + 1));
        }
        Scanner sc = new Scanner(System.in);


        while (true){
            System.out.println();
            System.out.println("=====================================");
            System.out.println("输入 0 退出银行系统...");
            System.out.println("选择你要管理的账户");

            // 直接转化为index，方便操作
            int index = (sc.nextInt() -1);
            // 退出系统
            if (index == -1) break;

            if (index<0 || index>4) {
                System.out.println();
                System.out.println("请输入正确的账户...");
                continue;
            }
            
            System.out.println("当前账户" + (index+1));
            System.out.println("余额 ---> " + arr[index]);
            System.out.println();
            System.out.println("----------------------开始管理账户-------------------------");
            System.out.println();
            while (true){

                System.out.println();
                System.out.println("=====================================");
                System.out.println("存款 1，取款 2，转账 3，结束请按 0 ...");
                System.out.println("请选择你需要的操作");
                int operation = sc.nextInt();

                if (operation == 0) break;
                if (operation < 0 || operation >3){
                    System.out.println();
                    System.out.println("请选择正确的操作代码...");
                    continue;
                }

                if (operation == 1) {
                    while (true) {
                        System.out.println();
                        System.out.println("=====================================");
                        System.out.println("退出请按 0");
                        System.out.println("请输入存款金额...");
                        int amount = sc.nextInt();
                        if (amount == 0) break;
                        if (amount < 0) {
                            System.out.println();
                            System.out.println("请输入正确的金额");
                            continue;
                        }
                        arr[index] += amount;
                        System.out.println();
                        System.out.println("账户：" + (index+1));
                        System.out.println("已存入：" + amount);
                        System.out.println("账户余额：" + arr[index]);
                    }
                } else if (operation == 2) {
                    while (true) {
                        System.out.println();
                        System.out.println("=====================================");
                        System.out.println("退出请按 0");
                        System.out.println("请输入取款金额...");
                        int amount = sc.nextInt();
                        if (amount == 0) break;
                        if (amount < 0) {
                            System.out.println();
                            System.out.println("请输入正确的金额");
                            continue;
                        }
                        if(amount > arr[index]){
                            System.out.println();
                            System.out.println("余额不足，请及时充值...");
                            continue;
                        }
                        arr[index] -= amount;
                        System.out.println();
                        System.out.println("账户：" + (index+1));
                        System.out.println("已取出：" + amount);
                        System.out.println("账户余额：" + arr[index]);
                    }
                } else {
                    while (true) {
                        System.out.println();
                        System.out.println("=====================================");
                        System.out.println("退出请按 0...");
                        System.out.println("请输入转存账户...");
                        int account = (sc.nextInt()-1);
                        if (account == -1) break;
                        if (account < 0 || account > 4) {
                            System.out.println();
                            System.out.println("请输入正确的账户...");
                            continue;
                        }
                        System.out.println();
                        System.out.println("请输入转账金额");
                        while (true) {
                            System.out.println();
                            System.out.println("=====================================");
                            System.out.println("退出请按 0");
                            System.out.println("请输入转账金额...");
                            int amount = sc.nextInt();
                            if (amount == 0) break;
                            if (amount < 0) {
                                System.out.println();
                                System.out.println("请输入正确的金额");
                                continue;
                            }
                            if(amount > arr[index]){
                                System.out.println();
                                System.out.println("余额不足，请及时充值...");
                                continue;
                            }
                            arr[index] -= amount;
                            arr[account] += amount;
                            System.out.println();
                            System.out.println("账户：" + (index+1));
                            System.out.println("已转出：" + amount);
                            System.out.println("账户余额：" + arr[index]);
                        }
                    }
                }


            }
            System.out.println();
            System.out.println("----------------------结束管理账户-------------------------");
            System.out.println();
        }

        System.out.println();
        System.out.println("+++++++++++++++++++++++++++安全退出系统+++++++++++++++++++++++++++");

        int sum = Arrays.stream(arr).sum();
        OptionalInt max = Arrays.stream(arr).max();
        System.out.println(" ------- 账户综合 -------");
        System.out.println(sum);
        System.out.println(" ------- 账户金额最大 -------");
        System.out.println(max);
    }
}
